﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class leftMenu : System.Web.UI.Page
{
    ZSql zs = new ZSql();
    ZSql zs1 = new ZSql();
    ZSql zs2 = new ZSql();
    ZSql info = new ZSql();
    ZSql menu = new ZSql();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            DataTable menu = GetMenu();
            LblTree.Text += "<script language=\"javascript\" type=\"text/jscript\"> \n";
            LblTree.Text += "ImgDir = \"leftimage/\";\n";
            LblTree.Text += "SetFolderIcon(\"desktop.gif\");";
            LblTree.Text += "treeMenuAddItem(0, \"&nbsp系统功能操作菜单\");\n";
            LblTree.Text += "SetFolderIcon();\n";
            LblTree.Text += "var MenuColor=top.MenuColor;\n";
            LblTree.Text += "var MenuTextColor=\"#000000\";\n";
           
            //zs.Open("select * from menu where parentid=0");
            //for (int i = 0; i < zs.m_table.Rows.Count; i++)
            //{
            //    LblTree.Text += "treeMenuAddItem(1, \"" + zs.m_table.Rows[i]["menu"] + "\");\n";

            //    zs1.Open("select * from menu where parentid=" + zs.m_table.Rows[i]["menuid"]);
            //    for (int j = 0; j < zs1.m_table.Rows.Count; j++)
            //    {
            //        if (zs1.m_table.Rows[j]["url"].ToString()!="")
            //        {
            //            LblTree.Text += "treeMenuAddItem(2, \"" + zs1.m_table.Rows[j]["menu"] + "\", \"" + zs1.m_table.Rows[j]["Url"] + "\", \"tabWin\",\"l4.gif\");\n";

            //        }
            //        else
            //        {
            //            LblTree.Text += "treeMenuAddItem(2, \"" + zs1.m_table.Rows[j]["menu"] + "1\");\n";
            //            zs2.Open("select * from menu where parentid=" + zs1.m_table.Rows[j]["menuid"]);
            //            for (int k = 0; k < zs2.m_table.Rows.Count; k++)
            //            {
            //                LblTree.Text += "treeMenuAddItem(3, \"" + zs2.m_table.Rows[k]["menu"] + "\", \"" + zs2.m_table.Rows[k]["Url"] + "\", \"tabWin\",\"l4.gif\");\n";
            //            }
            //            zs2.Close();
            //        }

            //    }
            //    zs1.Close();
            //}

            //zs.Close();

            for (int i = 0; i < menu.Rows.Count; i++)
            {
                if (menu.Rows[i]["parentid"].ToString() == "0")
                {
                    LblTree.Text += "treeMenuAddItem(1, \"" + menu.Rows[i]["menu"] + "\");\n";
              
                for (int j = 0; j < menu.Rows.Count; j++)
                {
                    if (menu.Rows[i]["menuid"].ToString() == menu.Rows[j]["parentid"].ToString())
                    {
                        if (menu.Rows[j]["url"].ToString() != "")
                        {
                            LblTree.Text += "treeMenuAddItem(2, \"" + menu.Rows[j]["menu"] + "\", \"" + menu.Rows[j]["Url"]+ "?moduid="+menu.Rows[j]["menuid"]+"\", \"tabWin\",\"l4.gif\");\n";
                        }
                        else
                        {
                            LblTree.Text += "treeMenuAddItem(2, \"" + menu.Rows[j]["menu"] + "\");\n";

                            for (int k = 0; k < menu.Rows.Count; k++)
                            {
                                if (menu.Rows[j]["menuid"].ToString() == menu.Rows[k]["parentid"].ToString())
                                {

                                    LblTree.Text += "treeMenuAddItem(3, \"" + menu.Rows[k]["menu"] + "\", \"" + menu.Rows[k]["Url"] + "?moduid=" + menu.Rows[k]["menuid"] + "\", \"tabWin\",\"l4.gif\");\n";
                                }
                            }
                            zs2.Close();
                        }

                    }
                }  
                }
            }

            LblTree.Text += "treeMenuAddItem(1, \"个人设置\");\n";
            LblTree.Text += "treeMenuAddItem(2, \"修改密码\", \"System/EditUserPas.aspx\", \"tabWin\",\"l4.gif\");\n";
            LblTree.Text += "</script>\n";
            LblTree.Text += "<script language=\"JavaScript\" type=\"text/jscript\">\n";
            LblTree.Text += "initializeDocument();\n";
            LblTree.Text += "clickOnNode(0);\n";
            LblTree.Text += "</script>\n";
        }
    }

    private DataTable GetMenu()
    {
        string sql = "select * from menu where 1=1";

        if (Server.UrlDecode(Request.Cookies["user"].Values["roleid"]) != "1")
        {
            string menuid = "0";

            info.Open("select permission from purview where roleid = " + Request.Cookies["user"].Values["roleid"] + " or userid = '" + Request.Cookies["user"].Values["id"] + "'");
            for (int i = 0; i < info.m_table.Rows.Count; i++)
            {
                menuid += "," + info.m_table.Rows[i]["permission"].ToString();//5-1-a-s-p-x
            }

            sql += " and menuid in (" + menuid + ")";
        }

        menu.Open(sql);

        DataTable dt = menu.m_table;
        menu.Close();

        return dt;
    }

}
